import 'dart:async';

import 'package:get/get.dart';
import 'package:xuance/bean/BanIpItemBean.dart';
import 'package:xuance/bean/GlobalType.dart';
import 'package:xuance/bean/ReceiveDatabean.dart';
import 'package:xuance/bean/global_event_bean.dart';
import 'package:xuance/bean/splunkDataBean.dart';
import 'package:xuance/main.dart';
import 'package:xuance/network/api/BanIPApi.dart';
import 'package:xuance/network/api/HomeApi.dart';
import 'package:xuance/util/common_util.dart';
import 'package:xuance/util/toast_util.dart';

import 'state.dart';

class BanListLogic extends GetxController {
  final BanListState state = BanListState();
  RxInt page = 1.obs;
  RxInt spage = 1.obs;
  int pageSize = 10;

  StreamSubscription<GlobalEventBean>? listener;
  @override
  void onInit() {
    super.onInit();
    listener = eventBus.on<GlobalEventBean>().listen((event) {
      if (event.type == GlobalType.updateDate) {
        var bean = event.data as ReceiveDataBean;
        state.dateType.value = bean.time;
        state.queryCondition.value = bean.txt;
        init();
      }
    });
    init();
  }

  void lastPage() {
    if (page.value <= 1) {
      page.value = 1;
      showToast("已经是最后一页");
    } else {
      page.value--;
      getChartBan(getTime(state.dateType.value));
    }
  }

  void nextPage() {
    if (page.value >= state.totalPage.value) {
      page.value = state.totalPage.value;
      showToast("已经是第一页");
    } else {
      page.value++;
      getChartBan(getTime(state.dateType.value));
    }
  }

  Future<void> getChartBan(List<String> time) async {
    try {
      var time = getTime(state.dateType.value);
      var blockips = await HomeApi.blockIp(
          startTime: time[0],
          endTime: time[1],
          queryCondition: state.queryCondition.value);
      BanIpItemBean? res = await BanIPApi.getList(
          startDate: time[0],
          endDate: time[1],
          page: page.value,
          queryCondition: state.queryCondition.value);

      state.blockipsBan.clear();
      state.blockipsBan.addAll(blockips);
      state.listBan.clear();
      state.listBan.addAll(res?.list ?? []);
      page.value = res?.pageNum?.toInt() ?? 0;
      state.totalPage.value = res?.pages?.toInt() ?? 0;
    } catch (e) {
      print("blockips加载失败:${e}");
    }
  }

  Future<void> refreshPa(List<String> time) async {
    try {
      SplunkDataBean? splunkData = await HomeApi.getSplunkData(
          startTime: time[0],
          endTime: time[1],
          currentPage: spage.value,
          riskLevelSort: state.riskLevelSort.value,
          createdTime: state.createdTime.value,
          queryCondition: state.queryCondition.value);
      state.splunkDataList.clear();
      state.splunkDataList.addAll(splunkData?.list ?? []);
      state.splunkPages.value = splunkData?.pages?.toInt() ?? 0;
      spage.value = splunkData?.pageNum?.toInt() ?? 0;
    } catch (e) {
      print("获取pa列表失败");
    }
  }

  void slastPage() {
    if (spage.value == 1) {
      showToast("已经是第一页");
    } else {
      spage--;
      refreshPa(getTime(state.dateType.value));
    }
  }

  void snextPage() {
    if (spage.value > state.splunkPages.value) {
      spage.value = state.splunkPages.value;
    } else if (spage.value == state.splunkPages.value) {
      showToast("已经是最后一页");
    } else {
      spage++;
      refreshPa(getTime(state.dateType.value));
    }
  }

  void levelTap() {
    state.createdTime.value = '';
    if (state.riskLevelSort.isEmpty || state.riskLevelSort.value == "asc") {
      state.riskLevelSort.value = "desc";
    } else {
      state.riskLevelSort.value = "asc";
    }
    refreshPa(getTime(state.dateType.value));
    print("state.riskLevelSort.value${state.riskLevelSort.value}");
  }

  void timeTap() {
    state.riskLevelSort.value = '';
    if (state.createdTime.isEmpty || state.createdTime.value == "asc") {
      state.createdTime.value = "desc";
    } else {
      state.createdTime.value = "asc";
    }
    refreshPa(getTime(state.dateType.value));
    print("state.createdTime.value${state.createdTime.value}");
  }

  void init() {
    var time = getTime(state.dateType.value);
    getChartBan(time);
    refreshPa(time);
  }

  @override
  void onClose() {
    super.onClose();
    listener?.cancel();
  }
}
